<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/utils/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions();
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
    },
    setOptions({ mode = "daily" } = {}) {
      this.chart.setOption({
        title: {
          top: "0",
          left: "center",
          text: "借条数量统计",
          textStyle: {
            align: "center",
            color: "#FFFFFF",
            fontSize: 22
          }
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          }
        },
        legend: {
          x: "center",
          y: "40px",
          textStyle: {
            color: "#000",
            fontSize: 13
          },
          icon: "circle",
          data: ["累计借条数", "累计签署完成借条数"]
        },
        grid: {
          right: "5%",
          bottom: "10%",
          left: "2%",
          top: "80px",
          containLabel: true
        },
        dataZoom: [
          {
            type: "slider",
            show: true,
            height: 20,
            left: "10%",
            right: "10%",
            bottom: "2%",
            start: 70,
            end: 100,
            textStyle: {
              color: "#000",
              fontSize: 11
            }
          },
          {
            type: "inside"
          }
        ],
        xAxis: {
          data: this.chartData.dateList,
          type: "category",
          name: "日期",
          nameTextStyle: {
            color: "#000"
          },
          axisLine: {
            lineStyle: {
              color: "#000"
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#000",
              fontSize: 12
            },
            //interval:0,
            rotate: 30
          }
        },

        yAxis: {
          type: "value",
          name: "数量",
          minInterval: 1,
          nameTextStyle: {
            color: "#000"
          },
          position: "left",
          axisLine: {
            show: true,
            lineStyle: {
              color: "#000"
            }
          },
          splitLine: {
            show: false
          },
          axisLabel: {
            color: "#000",
            fontSize: 12
          }
        },
        series: [
          {
            name: "累计借条数",

            type: "line",
            data:
              mode === "daily"
                ? this.chartData.CreateNumberFromDaily
                : this.chartData.CreateNumberFromTotal
          },
          {
            name: "累计签署完成借条数",
            type: "line",
            data:
              mode === "daily"
                ? this.chartData.EffectNumberFromDaily
                : this.chartData.EffectNumberFromTotal
          }
        ]
      });
    }
  }
};
</script>
